package modulo1.cgd;

import java.util.List;
import javax.persistence.Query;
import modulo1.cdp.Curso;
import modulo1.cdp.Disciplina;

public class DisciplinaDaoJpa extends DaoJpa<Disciplina> {

  public List<Disciplina> buscarNome(String nome) throws Exception {
    try {
      Query q = em.createQuery("SELECT d FROM Disciplina d WHERE LOWER(d.nome) LIKE LOWER('" + nome + "%') ");
      return q.getResultList();
    } catch (Exception ex) {
      throw ex;
    }
  }
  
  public List<Disciplina> buscarPeriodo(int periodo) throws Exception {
    try {
      Query q = em.createQuery("SELECT d FROM Disciplina d WHERE d.periodo < "+periodo);
      return q.getResultList();
    } catch (Exception ex) {
      throw ex;
    }
  }  
  
  public List<Disciplina> buscarPeriodoCurso(int periodo, Curso curso) throws Exception {
    try {
      Query q = em.createQuery("SELECT d FROM Disciplina d WHERE d.periodo < "+periodo+" AND d.curso.id = "+curso.getId());
      return q.getResultList();
    } catch (Exception ex) {
      throw ex;
    }
  }  
  
  public List<Disciplina> buscarCurso(Curso curso) throws Exception {
    try {
      Query q = em.createQuery("SELECT d FROM Disciplina d WHERE d.curso.id = "+curso.getId());
      return q.getResultList();
    } catch (Exception ex) {
      throw ex;
    }
  }  
}
